package org.vaadin.passwordfield;

import org.vaadin.passwordfield.client.ui.PasswordFieldUtils;

import com.vaadin.Application;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

public class PasswordFieldDemo extends Application {

    private static final long serialVersionUID = -7438276388708655790L;

    @Override
    public void init() {
        setMainWindow(new MainWindow());

    }

    private class MainWindow extends Window {
        MainWindow() {
            setCaption("PasswordField Demo");
            ((VerticalLayout) getContent()).setSpacing(true);
            {
                PasswordField tf = new PasswordField("Basic");
                addComponent(tf);
            }

            {
                PasswordField tf = new PasswordField("Custom CapsLock warning");
                tf.setCapsLockWarning("Märk att CapsLock är på");
                tf
                        .setFlagDescription("Flaggan ser alltid lika ut om du skrivit ditt lösenord rätt");
                addComponent(tf);
            }

            {
                PasswordField tf = new PasswordField("No CapsLock warning");
                tf.setCapsLockWarning(null);
                addComponent(tf);
            }

            {
                HorizontalLayout hz = new HorizontalLayout();
                hz.setSpacing(true);
                addComponent(hz);

                final Label flag = new Label();
                flag.setContentMode(Label.CONTENT_XHTML);

                final PasswordField tf = new PasswordField(
                        "Generate 'flag' on server also");
                tf.setImmediate(true);
                tf.addListener(new ValueChangeListener() {

                    private static final long serialVersionUID = 8576495913301316250L;

                    public void valueChange(ValueChangeEvent event) {
                        String[] c = PasswordFieldUtils.hashColors(""
                                + event.getProperty().getValue());

                        flag
                                .setValue("<span style=\"background-color:"
                                        + c[0]
                                        + ";\">&nbsp;</span><span style=\"background-color:"
                                        + c[1]
                                        + ";\">&nbsp;</span><span style=\"background-color:"
                                        + c[2] + ";\">&nbsp;</span> "
                                        + tf.getFlagDescription());
                        flag.setCaption("Your password flag");
                    }
                });

                hz.addComponent(tf);
                hz.addComponent(flag);
            }
        }
    }

}
